package com.piece.core.pay.client.impl.wxpay;

import com.piece.core.framework.exception.ErrorCode;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.pay.client.AbstractPayResponseMapping;
import com.piece.core.pay.constants.ExceptionPayConstants;
import java.util.Objects;

public class WXResponseMapping extends AbstractPayResponseMapping {

    /**
     * 错误码 - 成功
     */
    public static final String CODE_SUCCESS = "SUCCESS";
    public static final String MESSAGE_SUCCESS = "成功";

    @Override
    protected ErrorCode convert(String apiCode, String apiMsg) {
        if (Objects.equals(apiCode, CODE_SUCCESS)) {
            return ErrorCode.SUCCESS;
        }

        if (Objects.equals(apiCode, "FAIL")) {
            if (Objects.equals(apiMsg, "AppID不存在，请检查后再试")) {
                return new ErrorCode(500, ExceptionPayConstants.PAY_CONFIG, null);
            }
            if (Objects.equals(apiMsg, "签名错误，请检查后再试")
                || Objects.equals(apiMsg, "签名错误")) {
                return new ErrorCode(500, ExceptionPayConstants.PAY_CONFIG, null);
            }
        }
        if (Objects.equals(apiCode, "PARAM_ERROR")) {
            if (Objects.equals(apiMsg, "无效的openid")) {
                return new ErrorCode(500, ExceptionPayConstants.PAY_PARAM, null);
            }
        }
        if (Objects.equals(apiCode, "CustomErrorCode")) {
            if (StringUtil.contains(apiMsg, "必填字段")) {
                return new ErrorCode(500, ExceptionPayConstants.PAY_PARAM, null);
            }
        }
        return null;
    }
}
